<?php
class SiteModel extends Model {
	var $tableName = 'cms_site';
	
	/**
	 * 获取站点列表
	 */
	public function getSiteList($actstr) {
		if(($res = S('sitelist_'.$actstr.'_'.$_SESSION['mid'])) === false) {
			//得到内容列表的权限
			$rid = M('role_user') -> where("user_id =".$_SESSION['mid']) -> field("role_id") -> find();
			if($rid){
				$sids = M('access') ->join(" as a left join ".C ( 'DB_PREFIX' ) ."node as n on a.node_id = n.id")
				-> where(" a.role_id=".$rid["role_id"]." and a.level=3 and n.name='{$actstr}'") -> field("n.sid") -> select();
			}
			if(empty($sids)){
				$list = $this->order('siteid asc')->select();
			}else{
				foreach ($sids as $k => $v){
					$sids[$k] = $v['sid'];
				}
				$map['siteid'] = array('in',implode(",",$sids));
				$list = $this->where($map)->order('siteid asc')->select();
			}
			$arr = array ();
			foreach ( $list as $key => $value ) {
				$arr [] = array (
						'id' => $value['siteid'],
						'pId' => 0,
						'name' => $value ['name'],
						'open' => true
				);
			}
			S('sitelist_'.$actstr.'_'.$_SESSION['mid'],$arr);
			return json_encode($arr);
		} else {
			return json_encode($res);
		}
	}

	public function getSiteListArr($actstr) {
		//得到内容列表的权限
		$rid = M('role_user') -> where("user_id =".$_SESSION['mid']) -> field("role_id") -> find();
		if($rid){
			$sids = M('access') ->join(" as a left join ".C ( 'DB_PREFIX' ) ."node as n on a.node_id = n.id")
			-> where(" a.role_id=".$rid["role_id"]." and a.level=3 and n.name='{$actstr}'") -> field("n.sid") -> select();
		}
		if(empty($sids)){
			$list = $this->order('siteid asc')->select();
		}else{
			foreach ($sids as $k => $v){
				$sids[$k] = $v['sid'];
			}
			$map['siteid'] = array('in',implode(",",$sids));
			$list = $this->where($map)->order('siteid asc')->select();
		}
		return $list;
	}
	
	public function getSiteInfo($sid) {
        if (($templatePath = S('cache_templatePath_'.$sid)) === false) {
            $findSite = $this->where('siteid='.$sid)->find();
            $findSite['dirname'] = trim($findSite['dirname']);
            $dirname = empty($findSite['dirname'])?'':$findSite['dirname'].'/';
            $templatePath = './'.$dirname;
            S('cache_templatePath_'.$sid, $templatePath);
        }
		return $templatePath;
	}
	
	public function getSiteId($site_name) {
		if(!empty($site_name)) {
			$data['name'] = $site_name;
			$siteid = $this->where($data)->getField('siteid');
			return $siteid;
		} else {
			return 0;
		}
	}
	
	public function checkSiteName($siteid,$site_name) {
		$data ['siteid'] = array (
				'neq',
				$siteid
		);
		$data['name'] = $site_name;
		return $this->where($data)->find();
	}
	
	public function getSiteDomain($sid) {
		if (($domain = S('cache_site_domain_'.$sid)) === false) {
			$domain = $this -> where("siteid = ".$sid) ->getField("domain");
			if( strpos(trim($domain),"/") === 0){
				$domain = 'http://'.SITE_DOMAIN.$domain;
			}
			if(strrpos(trim($domain),"/") !== (strlen(trim($domain))-1)){
				$domain = $domain.'/';
			}
			S('cache_site_domain_'.$sid, $domain);
		}
		return $domain;
	}
}